package com.haoliangtian.ui.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.haoliangtian.R;
import com.haoliangtian.ui.login.LoginActivity;
import com.haoliangtian.ui.order.OrderViewModel;
import com.haoliangtian.utils.SharedPreferencesUtil;
import com.haoliangtian.view.MainViewPager;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主页<br>
 * 2020/5/21 2:02 PM
 *
 * @author 华软科技
 * @version 1.0
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "✨✨✨✨✨" + MainActivity.class.getName() + "✨✨✨✨✨";
    @BindView(R.id.view_pager)
    MainViewPager viewPager;
    //tab栏自动模式
    @BindView(R.id.tabs)
    TabLayout tabs;

    @BindView(R.id.fab)
    FloatingActionButton fab;
    //当前页面的索引
    private int pagePosition;
    //订单的viewModel
    private OrderViewModel orderViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        checkLogin();


        //获取ViewPager的适配器
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        viewPager.setAdapter(sectionsPagerAdapter);
        viewPager.stopNestedScroll();
        //tab栏关联ViewPager
        tabs.setupWithViewPager(viewPager);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pagePosition = position;
                //订单页面显示多功能按钮
                if (position == 3) {
                    fab.setVisibility(View.VISIBLE);
                } else {
                    fab.setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        initViewModel();
    }

    private void initViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
        //观察订单页面UI状态
        orderViewModel.getStatus().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                fab.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 检查用户是否登录
     */
    private void checkLogin() {

        String param = (String) SharedPreferencesUtil.getParam(getBaseContext(), getString(R.string.user_login), "0");
        if (param.equals("0")) {
            //去登录页面
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
        }
    }

    //点击多功能按钮
    @OnClick(R.id.fab)
    void more(View view) {
        switch (pagePosition) {
            case 3:
                Log.i(TAG, "点击多功能按钮：" + 3);
                //通知订单页面打开或关闭筛选条件
                orderViewModel.switchCondition();
                fab.setVisibility(View.GONE);
                break;
            default:
                Snackbar.make(view, "功能还未开放！", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            Log.i(TAG, "登录成功");
        }
    }
}